package com.example.food03;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.example.food03.bool.WxService;
import com.example.food03.bool.WxService2;
import com.example.food03.bool.WxServiceAdapter;
import com.example.food03.bool.WxServiceAdapter_2;
import com.example.food03.bool.WxServiceAdapter_3_3;
import com.example.food03.strongweb_bool.HTMLtool;
import com.example.food03.web_bool.Collected;
import com.example.food03.web_bool.Collection;
import com.example.food03.web_bool.Food;
import com.example.food03.web_bool.Order;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity3 extends AppCompatActivity {
    public GridView gridView33;
    public Button b1;
    public View view1;
    public  List<WxService2> list1_1 = new ArrayList<>(16);//存放bitmap类型的list数组
    private List<String> list1=new ArrayList<>();  //存放读取出来的图片路径
    public String list2[]= new String[100];//存放菜单名
    public double list3[]=new double[100];//存放菜单价格
    //以下均是接收线程数据所用到的
    String h3_1,orders=new String();
    public static String[] order3_1=new String[100];
    public static String[][] username3_1=new String[100][100];
    public static String[][] comment3_1=new String[100][100];
    public static int userid3_1[][]=new int[100][100];
    public static String h2=new String();
    public static String h2_2[]=new String[100];
    Order order=new Order();
    Gson gson=new Gson();
    public int ll=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int y=0;y<MainActivity.shop_foodid[MainActivity.ii].length;y++){
                    try {
                        h2 = HTMLtool.getHtml("http://192.168.43.126:8080/foodService/isCollected.do?user_id=" +
                                LoginActivity.pwd2+"&&shop_food_id="+MainActivity.shop_foodid[MainActivity.ii][y]+"&&flag=1");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                h2_2[y]=gson.fromJson(h2,Collected.class).getCollected()+"";
                    Log.d("qq5", gson.fromJson(h2,Collected.class).getCollected()+"");
                }
                for(int j=0;j<MainActivity.shop_foodid[MainActivity.ii].length+1;j++){
                try {
                    int u=MainActivity.shop_foodid[MainActivity.ii][j];
                   // Log.d("qeq",MainActivity.shop_foodid[MainActivity.ii][j]+"");
                    h3_1= HTMLtool.getHtml("http://192.168.43.126:8080/foodService22/getAllUserFoodOrder.do?food_id="+u);
                    if(h3_1!="[]"){
                        Log.d("qq00",MainActivity.ii+"");
                    orders=h3_1.substring(1, h3_1.length()-2);
                    order3_1 = orders.split("\\},");
                    for( int i = 0;i<order3_1.length;i++) {
                        order3_1[i] = order3_1[i] + "}";
                        order = gson.fromJson(order3_1[i], Order.class);
                        username3_1[j][i]=order.getUsername();
                        userid3_1[j][i]=order.getUser_id();
                        comment3_1[j][i]=order.getContent();
                        Log.d("qq4",order.getContent()+"");
                    }

                    }
                    else{
                        Log.d("uuuu2","MainActivity.ii");
                        username3_1=new String[100][100];
                        userid3_1=new int[100][100];
                        comment3_1=new String[100][100];
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                }
            }
        }).start();
        //初始化
        init();
    /*    Log.d("lll4",MainActivity.shop_foodid[MainActivity.gg][].length()+"");*/
        //进行放MainSctivity中传过来的菜单名
        for (int i=0;i<MainActivity.foodnames1_1.length;i++){
            list2[i]=MainActivity.foodnames1_1[i];
            Log.d("uuuu2",MainActivity.ii+"");
        }
        if (new File("/storage/emulated/0/店铺信息").exists()) {
            //该目录
            File files = new File("/storage/emulated/0/店铺信息");
            //存放目录下图片路径的数组
            File[] file = files.listFiles();
        if (files.exists()) {
            for (File f : file) {//通过for循环遍历获取到的文件数组
                list1.add(f.getPath());//将文件的路径添加到List1集合中
            }
            int k=MainActivity.ii;
            int j=0;
            for(int i = MainActivity.gg;i <MainActivity.foodshuliang[k]+MainActivity.gg;i++){
                Bitmap bm2 = BitmapFactory.decodeFile(list1.get(i));
                //imageView.setImageBitmap(bm2);
                //存放bitmap
              //  Log.d("jjj",MainActivity.shop_foodprice[k][j]+"");
               list3[i]=MainActivity.shop_foodprice[k][j];
               j++;
                list1_1.add(new WxService2(bm2,list2[i],list3[i]));
            }
               }

        }
        WxServiceAdapter_3_3 wxServiceAdapter2 =new WxServiceAdapter_3_3(MainActivity3.this,list1_1);
        gridView33.setAdapter(wxServiceAdapter2);

    }
    public void init(){
        view1=View.inflate(MainActivity3.this, R.layout.lu_3_3,null);
        b1=view1.findViewById(R.id.button3_1);
        gridView33=findViewById(R.id.grid33_1);
    }
}